@extends('home.shopcar.layout')
@section('shopcarlist')

    <div class="sc-pro-list">
        <!-- 20131223-订单-商品-标题-start -->
        <div class="sc-pro-title-area">
            <div class="h">
                <table border="0" cellpadding="0" cellspacing="0">
                    <thead>
                    <tr>
                        <th class="tr-check id">
                            编号
                        </th>
                        <th class="tr-pro">
                            商品
                        </th>
                        <th class="tr-price">
                            单价
                            <em>
                                （元）
                            </em>
                        </th>
                        <th class="tr-quantity">
                            数量
                        </th>
                        <th class="tr-subtotal">
                            小计
                            <em>
                                （元）
                            </em>
                        </th>
                        <th class="tr-operate">
                            操作
                        </th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
        <!-- 20131223-订单-商品-标题-end -->
        <form autocomplete="off" id="cart-form" method="POST">
            {{ csrf_field() }}
            <input name="state" type="hidden" value="1">
            <span id="cart-list">
                <!--product-list start-->
                <div class="sc-pro-area selected" id="order-pro-791219487">
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tbody>

                            @foreach($data as $k => $v)
                                <tr class="sc-pro-item">
                                <td width="50px" rowspan="1" align="center" class="id">
                                    {{ $v->id }}
                                </td>
                                <td class="tr-pro">
                                    <div class="pro-area clearfix">
                                        <p class="p-img">
                                            <a href="http://www.vmall.com/product/184868333.html#791219487"
                                               seed="cart-item-name" target="_blank" title="荣耀手表 S1 表体（深空灰）+长腕带（黑色)">
                                                <img alt="" src="{{ asset('/uploads/goodspic/') }}/{{ $v->goodspic }}"/>
                                            </a>
                                        </p>
                                        <p class="p-name">
                                            <a href="#" seed="cart-item-name" target="_blank" title="">
                                                {{ $v->goodsname }}
                                            </a>
                                        </p>
                                        <p class="p-sku">
                                            <em>
                                                {{ $v->system }}
                                            </em>
                                            <em>
                                                {{ $v->color }}
                                            </em>
                                            <em>
                                                {{ $v->rom }}
                                            </em>
                                        </p>
                                        <p class="understock-791219487 hide">
                                        </p>
                                    </div>
                                </td>
                                <td class="tr-price price">
                                        {{ $v->price }}元
                                </td>
                                <td rowspan="1" class="tr-quantity">
                                    <input type="button" class="jia" value="+" style="width:17px;height: 25px">
                                    <span id="number">{{$v->number}}</span>
                                    <input type="button" class="jian" value="-">
                                </td>
                                <td class="tr-subtotal xiaoji" rowspan="1">
                                        {{ ($v->price)*($v->number) }}元
                                </td>
                                <td class="tr-operate" rowspan="1">
                                    <a href="{{ url('/home/shopcar/delete/') }}/{{ $v->id }}">删除</a>
                                </td>
                                 </tr>
                            @endforeach

                        </tbody>
                    </table>
                </div>
                <!--product-list end-->
            </span>
            </input>
        </form>
    </div>
    <div class="hr-20">
    </div>
        <div class="sc-total-area clearfix" id="cart-total-area">
            <div class="sc-total-control">
            </div>
            <div class="sc-total-price">
                <table border="0" cellpadding="0" cellspacing="0">
                    <tbody>
                    <tr>
                        <th>
                            总计金额：
                        </th>
                        <td id="sc-cartInfo-totalOriginalPrice" class='zh'>
                            ¥{{ $sum}}
                        </td>
                    </tr>
                    <tr>
                        <th>
                            共节省：
                        </th>
                        <td id="sc-cartInfo-minusPrice">
                            ¥0.00
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <em>
                                合计(不含运费)：
                                <em>
                                </em>
                            </em>
                        </th>
                        <td>
                            <b id="sc-cartInfo-totalPrice" class="zh">
                                {{ $sum }}元
                            </b>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="hr-25">
        </div>
        <div class="sc-action-area clearfix">
            <a class="button-style-4 button-go-shopping-3" href="{{ url('/home/index/index') }}">
                继续购物
            </a>
            <form action="{{ url('/home/indent/confirm') }}" method="POST">
            {{ csrf_field() }}
            <input type="hidden" name="shopcar" value="{{ $sum }}" id="shopcar">
            <input type="hidden" name="flag" value="1">
            <button class="button-style-1 button-go-checkout-2" seed="cart-pay" id="btn">
                去结算
            </button>
            </form>
            <div class="sc-action-tips hide" id="sc-action-tips">
                <div class="tips-style-1 tips-area">
                    <i>
                    </i>
                    <div class="tips-text">
                        <p id="tips-text-p">
                            购物车中有库存不足商品，请处理后结算
                        </p>
                    </div>
                    <s>
                    </s>
                </div>
            </div>
        </div>
        <!--购物车-空数据 -->
        <div class="sc-empty-area hide" id="cart-empty-msg">
            亲，您购物车里还没有物品哦，快去逛逛吧！
        </div>
        <!-- 购物车列表 End-->
    <script type="text/javascript">
        window.onload = function () {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            
            $('.jia').click(function() {
                var t = $(this);
                // 获取当前商品的价格
                var price = parseInt(t.parent().parent().find('.price').html());
                // 获取当前小计一栏的对象，
                var xiaoji = t.parent().parent().find('.xiaoji');
                var id = parseInt(t.parent().parent().find('.id').html());
                var number = t.parent().parent().find('#number');
                var oldval = parseInt(number.html());
                var newval = oldval + 1;
                number.html(newval);
                $.ajax({
                    type:'GET',
                    url:"{{ url('/home/shopcar/doEdit') }}",
                    async:true,
                    data:{id:id,number:newval},
                    dataType:'json',
                    success:function(data) {
                        if(data) {
                            var sum = parseInt(data['sum']);
                            var newnumber = data['newnumber'];
                            var xj = newnumber*price;
                            xiaoji.html(xj+'元');
                            $('.zh').html(sum+'元');
                        } else {
                            alert('商品数量失败');
                        }
                    },
                })

            })
            $('.jian').click(function() {
                var t = $(this);
                // 获取当前商品的价格
                var price = parseInt(t.parent().parent().find('.price').html());
                // 获取当前小计一栏的对象，
                var xiaoji = t.parent().parent().find('.xiaoji');
                var id = parseInt(t.parent().parent().find('.id').html());
                var number = t.parent().parent().find('#number');
                var oldval = parseInt(number.html());
                var newval = oldval - 1;
                if(newval <= 0) {
                    newval = 1;
                }
                number.html(newval);
                $.ajax({
                    type:'GET',
                    url:"{{ url('/home/shopcar/doEdit') }}",
                    async:true,
                    data:{id:id,number:newval},
                    dataType:'json',
                    success:function(data) {
                        if(data) {
                            var sum = parseInt(data['sum']);
                            var newnumber = data['newnumber'];
                            var xj = newnumber*price;
                            xiaoji.html(xj+'元');
                            $('.zh').html(sum+'元');
                        } else {
                            alert('商品数最少为1');
                        }
                    },
                })

            })
        }
        if($('#shopcar').val() == 0) {
            $('#btn').attr('disabled', true);
        }
    </script>
@endsection
